package cbbx_sm.simulation;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;

import org.apache.commons.io.FileUtils;

import cbbx_sm.decision_maker.search.DynamicProgrammingLookahead;
import cbbx_sm.decision_maker.search.DynamicProgrammingTableEntry;
import cbbx_sm.decision_maker.search.EventFactory;
import cbbx_sm.decision_maker.search.Schedules;
import cbbx_sm.decision_maker.search.States;
import cbbx_sm.evaluation.CompareAlternativeSchedulers.Decision;
import cbbx_sm.probabilistic_model.Cluster;
import cbbx_sm.probabilistic_model.ProbabilisticModel;
import cbbx_sm.utils.LookaheadPredictorSerializator;

public class GenerateUPZOOMGraph {

	public static void main(String[] args) throws Exception{
		int numClusters = 4;
		ArrayList<String> cameraIds = new ArrayList<String>();
		cameraIds.add("cam13");
		cameraIds.add("cam14");
		cameraIds.add("cam15");
		cameraIds.add("cam16");
		cameraIds.add("cam17");
		cameraIds.add("cam18");
		
		// Load model to figure out which index was each cluster when single camera model was created.
		// Load the probabilistic model or create it.
		String camerasConcat = "";
		int i=0;
		for (String cam: cameraIds){
			camerasConcat+=cam+(i<(cameraIds.size()-1)?"_":"");
			i++;
		}
		String modelFileName = "model"+camerasConcat+"_"+"k="+numClusters+"_kmeans.ser";
		ProbabilisticModel probModel = ProbabilisticModel.readUp(modelFileName);
		ProbabilisticModel singleCamModel = probModel.projectModel("cam13");
		double utilityZoom = 1;
		double utilityUP = 0;
		int numberOfTimeStampsLookAhead = 10;
		String result = ("zoomu"+"\t"+"upu"+"\t"+"up"+"\t"+"other\n");
		double delta = 0.30000000000000004d;
		utilityUP = 0;
		for (; utilityUP <= 1; utilityUP += 0.1) {
			DynamicProgrammingLookahead camTable =
				LookaheadPredictorSerializator.readUpScoreArray(
						singleCamModel, utilityZoom, utilityUP, delta,
						numberOfTimeStampsLookAhead);
			if (camTable == null){
				LookaheadPredictorSerializator.generateLookaheadTable(singleCamModel, utilityZoom, utilityUP, numberOfTimeStampsLookAhead, delta);
			}
			camTable = LookaheadPredictorSerializator.readUpScoreArray(singleCamModel, utilityZoom, utilityUP, delta, numberOfTimeStampsLookAhead);
			int up = 0;
			int other = 0;
			for (i=0; i<camTable.getTable()[0].length; i++){
				if (camTable.getTable()[0][i].bestActionFloor.compareTo(Schedules.UP)==0){
					up++;
				} else {
					other++;
				}
			}
			double fraction = (double)up/(double)(other+up);
			result+=((float)utilityZoom+"\t"+(float)utilityUP+"\t"+up+"\t"+other+"\t"+String.format("%1$.2f", fraction)+"\n");
		}
		System.out.println(result);
		FileUtils.writeStringToFile(new File("output/zoomupres.txt"), result);
		
	}
	
	
//	/**
//	 * @param args
//	 * @throws Exception 
//	 */
//	public static void mainOld(String[] args) throws Exception {
//		String result = "";
//		String[] schedules = {"C0","C1","C2","C3",Schedules.UP};
//		double[] possibleValues = {0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1};
//		EventFactory.createEventInstance(possibleValues);
//		Schedules.setSchedulesAvailable(schedules);
//		States s = new States();
//		result+=("zoomu"+"\t"+"upu"+"\t"+"up"+"\t"+"other\n");
//		double begin  = 0;
//		//for (double begin = 0; begin<=0.05; begin+=0.05){
//			for (double zoomu = begin; zoomu<=1; zoomu+=0.1){
//				for (double upu = begin; upu<=1; upu+=0.1){
//					if (zoomu==0 && upu==0){
//						continue;
//					}
//					try {
//						String fileName = "output/policies/Cam13_Z_"+zoomu+"_U_"+upu+"Policy.txt";
//						int up = 0;
//						int other = 0;
//						String file = FileUtils.readFileToString(new File(fileName));
//						for (String line: file.substring(file.indexOf("\n")+1).split("\n")){
//							
//							if (s.get(stringToState(line.split("\t")[0]))==null){
//								continue;
//							}
//
//							if (line.split("\t")[2].compareTo(Schedules.UP)==0){
//								up++;
//							} else {
//								other++;
//							}
//							
//						}
//						result+=((float)zoomu+"\t"+(float)upu+"\t"+up+"\t"+other+"\n");
//					} catch (IOException e) {
//						e.printStackTrace();
//					}
//			//	}
//			}
//		}
//
//		System.out.println(result);
//		FileUtils.writeStringToFile(new File("output/zoomupres.txt"), result);
//	}

	private static double[] stringToState(String string) {
		double[] event = new double[string.split(":").length];
		int i=0;
		for (String d: string.split(":")){
			event[i] =  Double.parseDouble(d);
			i++;
		}
		return event;
	}

}
